--by Crazydru 
local BCStime
local Bonus=0

function QuickHeal_Priest_GetRatioHealthyExplanation()
    if QuickHealVariables.RatioHealthyPriest >= QuickHealVariables.RatioFull then
        return QUICKHEAL_SPELL_FLASH_HEAL .. " 总是在战斗中使用, 并且 "  .. QUICKHEAL_SPELL_LESSER_HEAL .. ", " .. QUICKHEAL_SPELL_HEAL .. " 或 " .. QUICKHEAL_SPELL_GREATER_HEAL .. " 将在脱战后使用. ";
    else
        if QuickHealVariables.RatioHealthyPriest > 0 then
            return QUICKHEAL_SPELL_FLASH_HEAL .. " 会在战斗中使用(如果目标低于 " .. QuickHealVariables.RatioHealthyPriest*100 .. "% 生命).否则会使用 " .. QUICKHEAL_SPELL_LESSER_HEAL .. ", " .. QUICKHEAL_SPELL_HEAL .. " 或 " .. QUICKHEAL_SPELL_GREATER_HEAL .. ". ";
        else
            return QUICKHEAL_SPELL_FLASH_HEAL .. " 永远不会被使用. " .. QUICKHEAL_SPELL_LESSER_HEAL .. ", " .. QUICKHEAL_SPELL_HEAL .. " 或 " .. QUICKHEAL_SPELL_GREATER_HEAL .. " 将永远在使用. ";
        end
    end
end

function QuickHeal_Priest_FindHealSpellToUse(Target, healType, multiplier, forceMaxHPS)
    local SpellID = nil;
    local HealSize = 0;
    local Overheal = false;

    -- Return immediately if no player needs healing
    if not Target then
        return SpellID,HealSize;
    end

    if multiplier == nil then
        jgpprint(">>> multiplier is NIL <<<")
        --if multiplier > 1.0 then
        --    Overheal = true;
        --end
    elseif multiplier == 1.0 then
        jgpprint(">>> multiplier is " .. multiplier .. " <<<")
    elseif multiplier > 1.0 then
        jgpprint(">>> multiplier is " .. multiplier .. " <<<")
        Overheal = true;
    end

    -- +Healing-PenaltyFactor = (1-((20-LevelLearnt)*0.0375)) for all spells learnt before level 20
    local PF1 = 0.2875;
    local PF4 = 0.4;
    local PF10 = 0.625;
    local PF18 = 0.925;

    -- Determine health and healneed of target
    local healneed;
    local Health;

    if QuickHeal_UnitHasHealthInfo(Target) then
    -- Full info available
        healneed = UnitHealthMax(Target) - UnitHealth(Target) - HealComm:getHeal(UnitName(Target)); -- Implementatin for HealComm
        if Overheal then
            healneed = healneed * multiplier;
        else
            --
        end
        Health = UnitHealth(Target) / UnitHealthMax(Target);
    else
        -- Estimate target health
        healneed = QuickHeal_EstimateUnitHealNeed(Target,true); -- needs HealComm implementation maybe
        if Overheal then
            healneed = healneed * multiplier;
        else
            --
        end
        Health = UnitHealth(Target)/100;
    end

    jgpprint(">>> healneed is " .. healneed .. " <<<")

--by Crazydru 增加BCS进行统计，kook区更新的BCS统计更准确

	BCStime=BCStime or GetTime()-20
	if GetTime()-BCStime>10 then
   	 -- if BonusScanner is running, get +Healing bonus
    		if (BonusScanner) then
        		Bonus = tonumber(BonusScanner:GetBonus("HEAL"));
        		QuickHeal_debug(string.format("Equipment Healing Bonus: %d", Bonus));
    		end
		if BCS then
			local power,_,_,dmg = BCS:GetSpellPower()
			local heal = BCS:GetHealingPower()
			if dmg == nil then
				power,_,_,dmg = BCS:GetLiveSpellPower()
				heal = BCS:GetLiveHealingPower()
			end				
            heal = heal or 0
            power = power or 0
            dmg = dmg or 0
        	Bonus = tonumber(heal)+tonumber(power)-tonumber(dmg);
       		QuickHeal_debug(string.format("装备治疗效果: %d", Bonus));
    		end
		BCStime=GetTime()
	end

    -- Spiritual Guidance - Increases spell damage and healing by up to 5% (per rank) of your total Spirit.
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,12);
    local _,Spirit,_,_ = UnitStat('player',5);
    local sgMod = Spirit * 5*talentRank/100;
    QuickHeal_debug(string.format("Spiritual Guidance Bonus: %f", sgMod));

    -- Calculate healing bonus
    local healMod15 = (1.5/3.5) * (sgMod + Bonus);
    local healMod20 = (2.0/3.5) * (sgMod + Bonus);
    local healMod25 = (2.5/3.5) * (sgMod + Bonus);
    local healMod30 = (3.0/3.5) * (sgMod + Bonus);
    QuickHeal_debug("Final Healing Bonus (1.5,2.0,2.5,3.0)", healMod15,healMod20,healMod25,healMod30);

    local InCombat = UnitAffectingCombat('player') or UnitAffectingCombat(Target);

    -- Spiritual Healing - Increases healing by 6% per rank on all spells
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,15);
    local shMod = 6*talentRank/100 + 1;
    QuickHeal_debug(string.format("Spiritual Healing modifier: %f", shMod));

    -- Improved Healing - Decreases mana usage by 5% per rank on LH,H and GH
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,11);
    local ihMod = 1 - 5*talentRank/100;
    QuickHeal_debug(string.format("Improved Healing modifier: %f", ihMod));

    local TargetIsHealthy = Health >= QuickHealVariables.RatioHealthyPriest;
    local ManaLeft = UnitMana('player');

    if TargetIsHealthy then
    QuickHeal_debug("Target is healthy",Health);
    end

    -- Detect proc of 'Hand of Edward the Odd' mace (next spell is instant cast)
    if QuickHeal_DetectBuff('player',"Spell_Holy_SearingLight") then
    QuickHeal_debug("BUFF: Hand of Edward the Odd (out of combat healing forced)");
    InCombat = false;
    end

    -- Detect Inner Focus or Spirit of Redemption (hack ManaLeft and healneed)
    if QuickHeal_DetectBuff('player',"Spell_Frost_WindWalkOn",1) or QuickHeal_DetectBuff('player',"Spell_Holy_GreaterHeal") then
        QuickHeal_debug("Inner Focus or Spirit of Redemption active");
        ManaLeft = UnitManaMax('player'); -- Infinite mana
        healneed = 10^6; -- Deliberate overheal (mana is free)
    end

    if Overheal then
        QuickHeal_debug("MOOOOOOOOOOOOOOOOOOOOOOOOLTIPLIER");
    else

    end


    -- Get total healing modifier (factor) caused by healing target debuffs
    local HDB = QuickHeal_GetHealModifier(Target);
    QuickHeal_debug("Target debuff healing modifier",HDB);
    healneed = healneed/HDB;

    -- Get a list of ranks available for all spells
    local SpellIDsLH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_LESSER_HEAL);
    local SpellIDsH  = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_HEAL);
    local SpellIDsGH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_GREATER_HEAL);
    local SpellIDsFH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_FLASH_HEAL);
    local SpellIDsR = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_RENEW);

    local maxRankLH = table.getn(SpellIDsLH);
    local maxRankH  = table.getn(SpellIDsH);
    local maxRankGH = table.getn(SpellIDsGH);
    local maxRankFH = table.getn(SpellIDsFH);
    local maxRankR = table.getn(SpellIDsR);

    QuickHeal_debug(string.format("Found LH up to rank %d, H up top rank %d, GH up to rank %d, FH up to rank %d, and R up to max rank %d", maxRankLH, maxRankH, maxRankGH, maxRankFH, maxRankR));

    --Get max HealRanks that are allowed to be used
    local downRankFH = QuickHealVariables.DownrankValueFH  -- rank for 1.5 sec heals
    local downRankNH = QuickHealVariables.DownrankValueNH -- rank for < 1.5 sec heals

    -- Compensation for health lost during combat
    local k=1.0;
    local K=1.0;
    if InCombat then
    k=0.9;
    K=0.8;
    end

    -- if healType = channel
    --jgpprint(healType)

    --if healType == "channel" and Overheal and healneed == 0 then
    --    SpellID = SpellIDsGH[5]; HealSize = 2080*shMod+healMod30;
    --end

    if healType == "channel" then
        jgpprint("CHANNEL HEAL: " .. healType)
        -- Find suitable SpellID based on the defined criteria
        if not InCombat or TargetIsHealthy or maxRankFH<1 then
            -- Not in combat or target is healthy so use the closest available mana efficient healing
            QuickHeal_debug(string.format("Not in combat or target healthy or no flash heal available, will use closest available LH, H or GH (not FH)"))
            if Health < QuickHealVariables.RatioFull then
                SpellID = SpellIDsLH[1]; HealSize = (53+healMod15*PF1)*shMod; -- Default to LH
                if healneed > (  84+healMod20*PF4 )*k*shMod and ManaLeft >=  45*ihMod and maxRankLH >=2 and downRankNH >= 2  and SpellIDsLH[2] then SpellID = SpellIDsLH[2]; HealSize = (  84+healMod20*PF4 )*shMod end
                if healneed > ( 154+healMod25*PF10)*K*shMod and ManaLeft >=  75*ihMod and maxRankLH >=3 and downRankNH >= 3  and SpellIDsLH[3] then SpellID = SpellIDsLH[3]; HealSize = ( 154+healMod25*PF10)*shMod end
                if healneed > ( 330+healMod30*PF18)*K*shMod and ManaLeft >= 155*ihMod and maxRankH  >=1 and downRankNH >= 4  and SpellIDsH[1]  then SpellID = SpellIDsH[1] ; HealSize = ( 330+healMod30*PF18)*shMod end
                if healneed > ( 476+healMod30     )*K*shMod and ManaLeft >= 205*ihMod and maxRankH  >=2 and downRankNH >= 5  and SpellIDsH[2]  then SpellID = SpellIDsH[2] ; HealSize = ( 476+healMod30     )*shMod end
                if healneed > ( 624+healMod30     )*K*shMod and ManaLeft >= 255*ihMod and maxRankH  >=3 and downRankNH >= 6  and SpellIDsH[3]  then SpellID = SpellIDsH[3] ; HealSize = ( 624+healMod30     )*shMod end
                if healneed > ( 667+healMod30     )*K*shMod and ManaLeft >= 305*ihMod and maxRankH  >=4 and downRankNH >= 7  and SpellIDsH[4]  then SpellID = SpellIDsH[4] ; HealSize = ( 667+healMod30     )*shMod end
                if healneed > ( 838+healMod30     )*K*shMod and ManaLeft >= 370*ihMod and maxRankGH >=1 and downRankNH >= 8  and SpellIDsGH[1] then SpellID = SpellIDsGH[1]; HealSize = ( 838+healMod30     )*shMod end
                if healneed > (1066+healMod30     )*K*shMod and ManaLeft >= 455*ihMod and maxRankGH >=2 and downRankNH >= 9  and SpellIDsGH[2] then SpellID = SpellIDsGH[2]; HealSize = (1066+healMod30     )*shMod end
                if healneed > (1328+healMod30     )*K*shMod and ManaLeft >= 545*ihMod and maxRankGH >=3 and downRankNH >= 10 and SpellIDsGH[3] then SpellID = SpellIDsGH[3]; HealSize = (1328+healMod30     )*shMod end
                if healneed > (1632+healMod30     )*K*shMod and ManaLeft >= 655*ihMod and maxRankGH >=4 and downRankNH >= 11 and SpellIDsGH[4] then SpellID = SpellIDsGH[4]; HealSize = (1632+healMod30     )*shMod end
                if healneed > (1768+healMod30     )*K*shMod and ManaLeft >= 710*ihMod and maxRankGH >=5 and downRankNH >= 12 and SpellIDsGH[5] then SpellID = SpellIDsGH[5]; HealSize = (1768+healMod30     )*shMod end
            end
        elseif not forceMaxHPS then
            -- In combat and target is unhealthy and player has flash heal
            QuickHeal_debug(string.format("In combat and target unhealthy and player has flash heal, will only use FH"));
            if Health < QuickHealVariables.RatioFull then
                SpellID = SpellIDsFH[1]; HealSize = (225+healMod15)*shMod; -- Default to FH
                if healneed > (297*shMod+healMod15)*k and ManaLeft >= 155 and maxRankFH >=2 and downRankFH >= 2 and SpellIDsFH[2] then SpellID = SpellIDsFH[2]; HealSize = (297+healMod15)*shMod end
                if healneed > (319*shMod+healMod15)*k and ManaLeft >= 185 and maxRankFH >=3 and downRankFH >= 3 and SpellIDsFH[3] then SpellID = SpellIDsFH[3]; HealSize = (319+healMod15)*shMod end
                if healneed > (387*shMod+healMod15)*k and ManaLeft >= 215 and maxRankFH >=4 and downRankFH >= 4 and SpellIDsFH[4] then SpellID = SpellIDsFH[4]; HealSize = (387+healMod15)*shMod end
                if healneed > (498*shMod+healMod15)*k and ManaLeft >= 265 and maxRankFH >=5 and downRankFH >= 5 and SpellIDsFH[5] then SpellID = SpellIDsFH[5]; HealSize = (498+healMod15)*shMod end
                if healneed > (618*shMod+healMod15)*k and ManaLeft >= 315 and maxRankFH >=6 and downRankFH >= 6 and SpellIDsFH[6] then SpellID = SpellIDsFH[6]; HealSize = (618+healMod15)*shMod end
                if healneed > (769*shMod+healMod15)*k and ManaLeft >= 380 and maxRankFH >=7 and downRankFH >= 7 and SpellIDsFH[7] then SpellID = SpellIDsFH[7]; HealSize = (769+healMod15)*shMod end
            end
        elseif forceMaxHPS then
            if ManaLeft >= 155 and maxRankFH >=2 and downRankFH >= 2 and SpellIDsFH[2] then SpellID = SpellIDsFH[2]; HealSize = (297+healMod15)*shMod end
            if ManaLeft >= 185 and maxRankFH >=3 and downRankFH >= 3 and SpellIDsFH[3] then SpellID = SpellIDsFH[3]; HealSize = (319+healMod15)*shMod end
            if ManaLeft >= 215 and maxRankFH >=4 and downRankFH >= 4 and SpellIDsFH[4] then SpellID = SpellIDsFH[4]; HealSize = (387+healMod15)*shMod end
            if ManaLeft >= 265 and maxRankFH >=5 and downRankFH >= 5 and SpellIDsFH[5] then SpellID = SpellIDsFH[5]; HealSize = (498+healMod15)*shMod end
            if ManaLeft >= 315 and maxRankFH >=6 and downRankFH >= 6 and SpellIDsFH[6] then SpellID = SpellIDsFH[6]; HealSize = (618+healMod15)*shMod end
            if ManaLeft >= 380 and maxRankFH >=7 and downRankFH >= 7 and SpellIDsFH[7] then SpellID = SpellIDsFH[7]; HealSize = (769+healMod15)*shMod end
        end
    end
    QuickHeal_debug(string.format("选用技能ID：%d，技能治疗量：%d",SpellID,HealSize*HDB))
    return SpellID,HealSize*HDB;
end

function QuickHeal_Priest_FindHealSpellToUseNoTarget(maxhealth, healDeficit, healType, multiplier, forceMaxHPS, forceMaxRank, hdb, incombat)
    local SpellID = nil;
    local HealSize = 0;
    local Overheal = false;

    if multiplier == nil then
        jgpprint(">>> multiplier is NIL <<<")
        --if multiplier > 1.0 then
        --    Overheal = true;
        --end
    elseif multiplier == 1.0 then
        jgpprint(">>> multiplier is " .. multiplier .. " <<<")
    elseif multiplier > 1.0 then
        jgpprint(">>> multiplier is " .. multiplier .. " <<<")
        Overheal = true;
    end

    -- +Healing-PenaltyFactor = (1-((20-LevelLearnt)*0.0375)) for all spells learnt before level 20
    local PF1 = 0.2875;
    local PF4 = 0.4;
    local PF10 = 0.625;
    local PF18 = 0.925;

    -- Determine health and heal need of target
    local healneed = healDeficit * multiplier;
    local Health = healDeficit / maxhealth;

--by Crazydru 增加BCS进行统计，kook区更新的BCS统计更准确

	BCStime=BCStime or GetTime()-20
	if GetTime()-BCStime>10 then
   	 -- if BonusScanner is running, get +Healing bonus
    		if (BonusScanner) then
        		Bonus = tonumber(BonusScanner:GetBonus("HEAL"));
        		QuickHeal_debug(string.format("Equipment Healing Bonus: %d", Bonus));
    		end
		if BCS then
			local power,_,_,dmg = BCS:GetSpellPower()
			local heal = BCS:GetHealingPower()
			if dmg == nil then
				power,_,_,dmg = BCS:GetLiveSpellPower()
				heal = BCS:GetLiveHealingPower()
			end				
            heal = heal or 0
            power = power or 0
            dmg = dmg or 0
        	Bonus = tonumber(heal)+tonumber(power)-tonumber(dmg);
       		QuickHeal_debug(string.format("装备治疗效果: %d", Bonus));
    		end
		BCStime=GetTime()
	end

    -- Spiritual Guidance - Increases spell damage and healing by up to 5% (per rank) of your total Spirit.
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,12);
    local _,Spirit,_,_ = UnitStat('player',5);
    local sgMod = Spirit * 5*talentRank/100;
    QuickHeal_debug(string.format("Spiritual Guidance Bonus: %f", sgMod));

    -- Calculate healing bonus
    local healMod15 = (1.5/3.5) * (sgMod + Bonus);
    local healMod20 = (2.0/3.5) * (sgMod + Bonus);
    local healMod25 = (2.5/3.5) * (sgMod + Bonus);
    local healMod30 = (3.0/3.5) * (sgMod + Bonus);
    QuickHeal_debug("Final Healing Bonus (1.5,2.0,2.5,3.0)", healMod15,healMod20,healMod25,healMod30);

    local InCombat = UnitAffectingCombat('player') or incombat;

    -- Spiritual Healing - Increases healing by 6% per rank on all spells
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,15);
    local shMod = 6*talentRank/100 + 1;
    QuickHeal_debug(string.format("Spiritual Healing modifier: %f", shMod));

    -- Improved Healing - Decreases mana usage by 5% per rank on LH,H and GH
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,11);
    local ihMod = 1 - 5*talentRank/100;
    QuickHeal_debug(string.format("Improved Healing modifier: %f", ihMod));

    local TargetIsHealthy = Health >= QuickHealVariables.RatioHealthyPriest;
    local ManaLeft = UnitMana('player');

    if TargetIsHealthy then
        QuickHeal_debug("Target is healthy",Health);
    end

    -- Detect proc of 'Hand of Edward the Odd' mace (next spell is instant cast)
    if QuickHeal_DetectBuff('player',"Spell_Holy_SearingLight") then
        QuickHeal_debug("BUFF: Hand of Edward the Odd (out of combat healing forced)");
        InCombat = false;
    end

    -- Detect Inner Focus or Spirit of Redemption (hack ManaLeft and healneed)
    if QuickHeal_DetectBuff('player',"Spell_Frost_WindWalkOn",1) or QuickHeal_DetectBuff('player',"Spell_Holy_GreaterHeal") then
        QuickHeal_debug("Inner Focus or Spirit of Redemption active");
        ManaLeft = UnitManaMax('player'); -- Infinite mana
        healneed = 10^6; -- Deliberate overheal (mana is free)
    end

    -- Get total healing modifier (factor) caused by healing target debuffs
    --local HDB = QuickHeal_GetHealModifier(Target);
    --QuickHeal_debug("Target debuff healing modifier",HDB);
    healneed = healneed/hdb;

    -- Get a list of ranks available for all spells
    local SpellIDsLH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_LESSER_HEAL);
    local SpellIDsH  = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_HEAL);
    local SpellIDsGH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_GREATER_HEAL);
    local SpellIDsFH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_FLASH_HEAL);
    local SpellIDsR = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_RENEW);

    local maxRankLH = table.getn(SpellIDsLH);
    local maxRankH  = table.getn(SpellIDsH);
    local maxRankGH = table.getn(SpellIDsGH);
    local maxRankFH = table.getn(SpellIDsFH);
    local maxRankR = table.getn(SpellIDsR);

    QuickHeal_debug(string.format("Found LH up to rank %d, H up top rank %d, GH up to rank %d, FH up to rank %d, and R up to max rank %d", maxRankLH, maxRankH, maxRankGH, maxRankFH, maxRankR));

    --Get max HealRanks that are allowed to be used
    local downRankFH = QuickHealVariables.DownrankValueFH  -- rank for 1.5 sec heals
    local downRankNH = QuickHealVariables.DownrankValueNH -- rank for < 1.5 sec heals

    -- Compensation for health lost during combat
    local k=1.0;
    local K=1.0;
    if InCombat then
        k=0.9;
        K=0.8;
    end

    if not forceMaxHPS then
        SpellID = SpellIDsLH[1]; HealSize = (53+healMod15*PF1)*shMod; -- Default to LH
        if healneed > (  84+healMod20*PF4 )*k*shMod and ManaLeft >=  45*ihMod and maxRankLH >=2 and downRankNH >= 2  and SpellIDsLH[2] then SpellID = SpellIDsLH[2]; HealSize = (  84+healMod20*PF4 )*shMod end
        if healneed > ( 154+healMod25*PF10)*K*shMod and ManaLeft >=  75*ihMod and maxRankLH >=3 and downRankNH >= 3  and SpellIDsLH[3] then SpellID = SpellIDsLH[3]; HealSize = ( 154+healMod25*PF10)*shMod end
        if healneed > ( 330+healMod30*PF18)*K*shMod and ManaLeft >= 155*ihMod and maxRankH  >=1 and downRankNH >= 4  and SpellIDsH[1]  then SpellID = SpellIDsH[1] ; HealSize = ( 330+healMod30*PF18)*shMod end
        if healneed > ( 476+healMod30     )*K*shMod and ManaLeft >= 205*ihMod and maxRankH  >=2 and downRankNH >= 5  and SpellIDsH[2]  then SpellID = SpellIDsH[2] ; HealSize = ( 476+healMod30     )*shMod end
        if healneed > ( 624+healMod30     )*K*shMod and ManaLeft >= 255*ihMod and maxRankH  >=3 and downRankNH >= 6  and SpellIDsH[3]  then SpellID = SpellIDsH[3] ; HealSize = ( 624+healMod30     )*shMod end
        if healneed > ( 667+healMod30     )*K*shMod and ManaLeft >= 305*ihMod and maxRankH  >=4 and downRankNH >= 7  and SpellIDsH[4]  then SpellID = SpellIDsH[4] ; HealSize = ( 667+healMod30     )*shMod end
        if healneed > ( 838+healMod30     )*K*shMod and ManaLeft >= 370*ihMod and maxRankGH >=1 and downRankNH >= 8  and SpellIDsGH[1] then SpellID = SpellIDsGH[1]; HealSize = ( 838+healMod30     )*shMod end
        if healneed > (1066+healMod30     )*K*shMod and ManaLeft >= 455*ihMod and maxRankGH >=2 and downRankNH >= 9  and SpellIDsGH[2] then SpellID = SpellIDsGH[2]; HealSize = (1066+healMod30     )*shMod end
        if healneed > (1328+healMod30	  )*K*shMod and ManaLeft >= 545*ihMod and maxRankGH >=3 and downRankNH >= 10 and SpellIDsGH[3] then SpellID = SpellIDsGH[3]; HealSize = (1328+healMod30     )*shMod end
        if healneed > (1632+healMod30     )*K*shMod and ManaLeft >= 655*ihMod and maxRankGH >=4 and downRankNH >= 11 and SpellIDsGH[4] then SpellID = SpellIDsGH[4]; HealSize = (1632+healMod30     )*shMod end
        if healneed > (1768+healMod30     )*K*shMod and ManaLeft >= 710*ihMod and maxRankGH >=5 and downRankNH >= 12 and SpellIDsGH[5] then SpellID = SpellIDsGH[5]; HealSize = (1768+healMod30     )*shMod end
    else
        SpellID = SpellIDsFH[1]; HealSize = (225+healMod15)*shMod; -- Default to FH
        if healneed > (297+healMod15)*k*shMod and ManaLeft >= 155 and maxRankFH >=2 and downRankFH >= 2 and SpellIDsFH[2] then SpellID = SpellIDsFH[2]; HealSize = (297+healMod15)*shMod end
        if healneed > (319+healMod15)*k*shMod and ManaLeft >= 185 and maxRankFH >=3 and downRankFH >= 3 and SpellIDsFH[3] then SpellID = SpellIDsFH[3]; HealSize = (319+healMod15)*shMod end
        if healneed > (387+healMod15)*k*shMod and ManaLeft >= 215 and maxRankFH >=4 and downRankFH >= 4 and SpellIDsFH[4] then SpellID = SpellIDsFH[4]; HealSize = (387+healMod15)*shMod end
        if healneed > (498+healMod15)*k*shMod and ManaLeft >= 265 and maxRankFH >=5 and downRankFH >= 5 and SpellIDsFH[5] then SpellID = SpellIDsFH[5]; HealSize = (498+healMod15)*shMod end
        if healneed > (618+healMod15)*k*shMod and ManaLeft >= 315 and maxRankFH >=6 and downRankFH >= 6 and SpellIDsFH[6] then SpellID = SpellIDsFH[6]; HealSize = (618+healMod15)*shMod end
        if healneed > (769+healMod15)*k*shMod and ManaLeft >= 380 and maxRankFH >=7 and downRankFH >= 7 and SpellIDsFH[7] then SpellID = SpellIDsFH[7]; HealSize = (769+healMod15)*shMod end
    end
    QuickHeal_debug(string.format("选用技能ID：%d，技能治疗量：%d",SpellID,HealSize*HDB))
    return SpellID,HealSize*hdb;
end

function QuickHeal_Priest_FindHoTSpellToUse(Target, healType, forceMaxRank)
    local SpellID = nil;
    local HealSize = 0;
    -- Return immediatly if no player needs healing
    if not Target then
        QuickHeal_debug(string.format("选用技能ID：%d，技能治疗量：%d",SpellID,HealSize*HDB))
        return SpellID,HealSize;
    end

    -- +Healing-PenaltyFactor = (1-((20-LevelLearnt)*0.0375)) for all spells learnt before level 20
    local PF1 = 0.2875;
    local PF4 = 0.4;
    local PF10 = 0.625;
    local PF18 = 0.925;

    -- Determine health and healneed of target
    local healneed;
    local Health;

    if QuickHeal_UnitHasHealthInfo(Target) then
        -- Full info available
        healneed = UnitHealthMax(Target) - UnitHealth(Target) - HealComm:getHeal(UnitName(Target)); -- Implementatin for HealComm
        Health = UnitHealth(Target) / UnitHealthMax(Target);
    else
        -- Estimate target health
        healneed = QuickHeal_EstimateUnitHealNeed(Target,true); -- needs HealComm implementation maybe
        Health = UnitHealth(Target)/100;
    end

--by Crazydru 增加BCS进行统计，kook区更新的BCS统计更准确

	BCStime=BCStime or GetTime()-20
	if GetTime()-BCStime>10 then
   	 -- if BonusScanner is running, get +Healing bonus
    		if (BonusScanner) then
        		Bonus = tonumber(BonusScanner:GetBonus("HEAL"));
        		QuickHeal_debug(string.format("Equipment Healing Bonus: %d", Bonus));
    		end
		if BCS then
			local power,_,_,dmg = BCS:GetSpellPower()
			local heal = BCS:GetHealingPower()
			if dmg == nil then
				power,_,_,dmg = BCS:GetLiveSpellPower()
				heal = BCS:GetLiveHealingPower()
			end				
            heal = heal or 0
            power = power or 0
            dmg = dmg or 0
        	Bonus = tonumber(heal)+tonumber(power)-tonumber(dmg);
       		QuickHeal_debug(string.format("装备治疗效果: %d", Bonus));
    		end
		BCStime=GetTime()
	end

    -- Spiritual Guidance - Increases spell damage and healing by up to 5% (per rank) of your total Spirit.
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,12);
    local _,Spirit,_,_ = UnitStat('player',5);
    local sgMod = Spirit * 5*talentRank/100;
    QuickHeal_debug(string.format("Spiritual Guidance Bonus: %f", sgMod));

    -- Calculate healing bonus
    local healMod15 = (1.5/3.5) * (sgMod + Bonus);
    local healMod20 = (2.0/3.5) * (sgMod + Bonus);
    local healMod25 = (2.5/3.5) * (sgMod + Bonus);
    local healMod30 = (3.0/3.5) * (sgMod + Bonus);
    QuickHeal_debug("Final Healing Bonus (1.5,2.0,2.5,3.0)", healMod15,healMod20,healMod25,healMod30);

    local InCombat = UnitAffectingCombat('player') or UnitAffectingCombat(Target);

    -- Spiritual Healing - Increases healing by 6% per rank on all spells
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,15);
    local shMod = 6*talentRank/100 + 1;
    QuickHeal_debug(string.format("Spiritual Healing modifier: %f", shMod));

    -- Improved Healing - Decreases mana usage by 5% per rank on LH,H and GH
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,11);
    local ihMod = 1 - 5*talentRank/100;
    QuickHeal_debug(string.format("Improved Healing modifier: %f", ihMod));

    local TargetIsHealthy = Health >= QuickHealVariables.RatioHealthyPriest;
    local ManaLeft = UnitMana('player');

    if TargetIsHealthy then
        QuickHeal_debug("Target is healthy",Health);
    end

    -- Detect proc of 'Hand of Edward the Odd' mace (next spell is instant cast)
    if QuickHeal_DetectBuff('player',"Spell_Holy_SearingLight") then
        QuickHeal_debug("BUFF: Hand of Edward the Odd (out of combat healing forced)");
        InCombat = false;
    end

    -- Detect Inner Focus or Spirit of Redemption (hack ManaLeft and healneed)
    if QuickHeal_DetectBuff('player',"Spell_Frost_WindWalkOn",1) or QuickHeal_DetectBuff('player',"Spell_Holy_GreaterHeal") then
        QuickHeal_debug("Inner Focus or Spirit of Redemption active");
        ManaLeft = UnitManaMax('player'); -- Infinite mana
        healneed = 10^6; -- Deliberate overheal (mana is free)
    end

    -- Get total healing modifier (factor) caused by healing target debuffs
    local HDB = QuickHeal_GetHealModifier(Target);
    QuickHeal_debug("Target debuff healing modifier",HDB);
    healneed = healneed/HDB;

    -- Get a list of ranks available for all spells
    local SpellIDsLH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_LESSER_HEAL);
    local SpellIDsH  = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_HEAL);
    local SpellIDsGH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_GREATER_HEAL);
    local SpellIDsFH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_FLASH_HEAL);
    local SpellIDsR = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_RENEW);

    local maxRankLH = table.getn(SpellIDsLH);
    local maxRankH  = table.getn(SpellIDsH);
    local maxRankGH = table.getn(SpellIDsGH);
    local maxRankFH = table.getn(SpellIDsFH);
    local maxRankR = table.getn(SpellIDsR);

    QuickHeal_debug(string.format("Found LH up to rank %d, H up top rank %d, GH up to rank %d, FH up to rank %d, and R up to max rank %d", maxRankLH, maxRankH, maxRankGH, maxRankFH, maxRankR));

    --Get max HealRanks that are allowed to be used
    local downRankFH = QuickHealVariables.DownrankValueFH  -- rank for 1.5 sec heals
    local downRankNH = QuickHealVariables.DownrankValueNH -- rank for < 1.5 sec heals

    -- Compensation for health lost during combat
    local k=1.0;
    local K=1.0;
    if InCombat then
        k=0.9;
        K=0.8;
    end

    -- if healType = channel
    jgpprint(healType)

    if healType == "channel" then
        jgpprint("CHANNEL HEAL: " .. healType)
        -- Find suitable SpellID based on the defined criteria
        if not InCombat or TargetIsHealthy or maxRankFH<1 then
            -- Not in combat or target is healthy so use the closest available mana efficient healing
            QuickHeal_debug(string.format("Not in combat or target healthy or no flash heal available, will use closest available LH, H or GH (not FH)"))
            if Health < QuickHealVariables.RatioFull then
                SpellID = SpellIDsLH[1]; HealSize = (53+healMod15*PF1)*shMod; -- Default to LH
                if healneed > (  84+healMod20*PF4 )*k*shMod and ManaLeft >=  45*ihMod and maxRankLH >=2 and downRankNH >= 2  and SpellIDsLH[2] then SpellID = SpellIDsLH[2]; HealSize = (  84+healMod20*PF4 )*shMod end
                if healneed > ( 154+healMod25*PF10)*K*shMod and ManaLeft >=  75*ihMod and maxRankLH >=3 and downRankNH >= 3  and SpellIDsLH[3] then SpellID = SpellIDsLH[3]; HealSize = ( 154+healMod25*PF10)*shMod end
                if healneed > ( 330+healMod30*PF18)*K*shMod and ManaLeft >= 155*ihMod and maxRankH  >=1 and downRankNH >= 4  and SpellIDsH[1]  then SpellID = SpellIDsH[1] ; HealSize = ( 330+healMod30*PF18)*shMod end
                if healneed > ( 476+healMod30     )*K*shMod and ManaLeft >= 205*ihMod and maxRankH  >=2 and downRankNH >= 5  and SpellIDsH[2]  then SpellID = SpellIDsH[2] ; HealSize = ( 476+healMod30     )*shMod end
                if healneed > ( 624+healMod30     )*K*shMod and ManaLeft >= 255*ihMod and maxRankH  >=3 and downRankNH >= 6  and SpellIDsH[3]  then SpellID = SpellIDsH[3] ; HealSize = ( 624+healMod30     )*shMod end
                if healneed > ( 667+healMod30     )*K*shMod and ManaLeft >= 305*ihMod and maxRankH  >=4 and downRankNH >= 7  and SpellIDsH[4]  then SpellID = SpellIDsH[4] ; HealSize = ( 667+healMod30     )*shMod end
                if healneed > ( 838+healMod30     )*K*shMod and ManaLeft >= 370*ihMod and maxRankGH >=1 and downRankNH >= 8  and SpellIDsGH[1] then SpellID = SpellIDsGH[1]; HealSize = ( 838+healMod30     )*shMod end
                if healneed > (1066+healMod30     )*K*shMod and ManaLeft >= 455*ihMod and maxRankGH >=2 and downRankNH >= 9  and SpellIDsGH[2] then SpellID = SpellIDsGH[2]; HealSize = (1066+healMod30     )*shMod end
                if healneed > (1328+healMod30     )*K*shMod and ManaLeft >= 545*ihMod and maxRankGH >=3 and downRankNH >= 10 and SpellIDsGH[3] then SpellID = SpellIDsGH[3]; HealSize = (1328+healMod30     )*shMod end
                if healneed > (1632+healMod30     )*K*shMod and ManaLeft >= 655*ihMod and maxRankGH >=4 and downRankNH >= 11 and SpellIDsGH[4] then SpellID = SpellIDsGH[4]; HealSize = (1632+healMod30     )*shMod end
                if healneed > (1768+healMod30     )*K*shMod and ManaLeft >= 710*ihMod and maxRankGH >=5 and downRankNH >= 12 and SpellIDsGH[5] then SpellID = SpellIDsGH[5]; HealSize = (1768+healMod30     )*shMod end
            end
        else
            -- In combat and target is unhealthy and player has flash heal
            QuickHeal_debug(string.format("In combat and target unhealthy and player has flash heal, will only use FH"));
            if Health < QuickHealVariables.RatioFull then
                SpellID = SpellIDsFH[1]; HealSize = (225+healMod15)*shMod; -- Default to FH
                if healneed > (297+healMod15)*k*shMod and ManaLeft >= 155 and maxRankFH >=2 and downRankFH >= 2 and SpellIDsFH[2] then SpellID = SpellIDsFH[2]; HealSize = (297+healMod15)*shMod end
                if healneed > (319+healMod15)*k*shMod and ManaLeft >= 185 and maxRankFH >=3 and downRankFH >= 3 and SpellIDsFH[3] then SpellID = SpellIDsFH[3]; HealSize = (319+healMod15)*shMod end
                if healneed > (387+healMod15)*k*shMod and ManaLeft >= 215 and maxRankFH >=4 and downRankFH >= 4 and SpellIDsFH[4] then SpellID = SpellIDsFH[4]; HealSize = (387+healMod15)*shMod end
                if healneed > (498+healMod15)*k*shMod and ManaLeft >= 265 and maxRankFH >=5 and downRankFH >= 5 and SpellIDsFH[5] then SpellID = SpellIDsFH[5]; HealSize = (498+healMod15)*shMod end
                if healneed > (618+healMod15)*k*shMod and ManaLeft >= 315 and maxRankFH >=6 and downRankFH >= 6 and SpellIDsFH[6] then SpellID = SpellIDsFH[6]; HealSize = (618+healMod15)*shMod end
                if healneed > (769+healMod15)*k*shMod and ManaLeft >= 380 and maxRankFH >=7 and downRankFH >= 7 and SpellIDsFH[7] then SpellID = SpellIDsFH[7]; HealSize = (769+healMod15)*shMod end
            end
        end
    end

    if healType == "hot" then
        QuickHeal_debug(string.format("Spiritual Healing modifier: %f", shMod));
        --SpellID = SpellIDsR[1]; HealSize = 215*shMod+healMod15; -- Default to Renew

        --if Health < QuickHealVariables.RatioFull then
        --if Health > QuickHealVariables.RatioHealthyPriest then
        if not forceMaxRank then
            SpellID = SpellIDsR[1]; HealSize = (45+healMod15)*shMod; -- Default to Renew(Rank 1)
            if healneed > (100+healMod15)*k*shMod and ManaLeft >= 155 and maxRankR >=2  and SpellIDsR[2]  then SpellID = SpellIDsR[2];  HealSize = (100+healMod15)*shMod end
            if healneed > (175+healMod15)*k*shMod and ManaLeft >= 185 and maxRankR >=3  and SpellIDsR[3]  then SpellID = SpellIDsR[3];  HealSize = (175+healMod15)*shMod end
            if healneed > (245+healMod15)*k*shMod and ManaLeft >= 215 and maxRankR >=4  and SpellIDsR[4]  then SpellID = SpellIDsR[4];  HealSize = (245+healMod15)*shMod end
            if healneed > (270+healMod15)*k*shMod and ManaLeft >= 265 and maxRankR >=5  and SpellIDsR[5]  then SpellID = SpellIDsR[5];  HealSize = (270+healMod15)*shMod end
            if healneed > (340+healMod15)*k*shMod and ManaLeft >= 315 and maxRankR >=6  and SpellIDsR[6]  then SpellID = SpellIDsR[6];  HealSize = (340+healMod15)*shMod end
            if healneed > (435+healMod15)*k*shMod and ManaLeft >= 380 and maxRankR >=7  and SpellIDsR[7]  then SpellID = SpellIDsR[7];  HealSize = (435+healMod15)*shMod end
            if healneed > (555+healMod15)*k*shMod and ManaLeft >= 455 and maxRankR >=8  and SpellIDsR[8]  then SpellID = SpellIDsR[8];  HealSize = (555+healMod15)*shMod end
            if healneed > (690+healMod15)*k*shMod and ManaLeft >= 545 and maxRankR >=9  and SpellIDsR[9]  then SpellID = SpellIDsR[9];  HealSize = (690+healMod15)*shMod end
            if healneed > (825+healMod15)*k*shMod and ManaLeft >= 655 and maxRankR >=10 and SpellIDsR[10] then SpellID = SpellIDsR[10]; HealSize = (825+healMod15)*shMod end
        else
            SpellID = SpellIDsR[10]; HealSize = (825+healMod15)*shMod
            if maxRankR >=2  and SpellIDsR[2]  then SpellID = SpellIDsR[2];  HealSize = (100+healMod15)*shMod end
            if maxRankR >=3  and SpellIDsR[3]  then SpellID = SpellIDsR[3];  HealSize = (175+healMod15)*shMod end
            if maxRankR >=4  and SpellIDsR[4]  then SpellID = SpellIDsR[4];  HealSize = (245+healMod15)*shMod end
            if maxRankR >=5  and SpellIDsR[5]  then SpellID = SpellIDsR[5];  HealSize = (270+healMod15)*shMod end
            if maxRankR >=6  and SpellIDsR[6]  then SpellID = SpellIDsR[6];  HealSize = (340+healMod15)*shMod end
            if maxRankR >=7  and SpellIDsR[7]  then SpellID = SpellIDsR[7];  HealSize = (435+healMod15)*shMod end
            if maxRankR >=8  and SpellIDsR[8]  then SpellID = SpellIDsR[8];  HealSize = (555+healMod15)*shMod end
            if maxRankR >=9  and SpellIDsR[9]  then SpellID = SpellIDsR[9];  HealSize = (690+healMod15)*shMod end
            if maxRankR >=10 and SpellIDsR[10] then SpellID = SpellIDsR[10]; HealSize = (825+healMod15)*shMod end
        end
        --end
    end

    QuickHeal_debug(string.format("选用技能ID：%d，技能治疗量：%d",SpellID,HealSize*HDB))
    return SpellID,HealSize*HDB;
end

function QuickHeal_Priest_FindHoTSpellToUseNoTarget(maxhealth, healDeficit, healType, multiplier, forceMaxHPS, forceMaxRank, hdb, incombat)
    local SpellID = nil;
    local HealSize = 0;

    -- +Healing-PenaltyFactor = (1-((20-LevelLearnt)*0.0375)) for all spells learnt before level 20
    local PF1 = 0.2875;
    local PF4 = 0.4;
    local PF10 = 0.625;
    local PF18 = 0.925;

    -- Determine health and heal need of target
    local healneed = healDeficit * multiplier;
    local Health = healDeficit / maxhealth;

--by Crazydru 增加BCS进行统计，kook区更新的BCS统计更准确

	BCStime=BCStime or GetTime()-20
	if GetTime()-BCStime>10 then
   	 -- if BonusScanner is running, get +Healing bonus
    		if (BonusScanner) then
        		Bonus = tonumber(BonusScanner:GetBonus("HEAL"));
        		QuickHeal_debug(string.format("Equipment Healing Bonus: %d", Bonus));
    		end
		if BCS then
			local power,_,_,dmg = BCS:GetSpellPower()
			local heal = BCS:GetHealingPower()
			if dmg == nil then
				power,_,_,dmg = BCS:GetLiveSpellPower()
				heal = BCS:GetLiveHealingPower()
			end				
            heal = heal or 0
            power = power or 0
            dmg = dmg or 0
        	Bonus = tonumber(heal)+tonumber(power)-tonumber(dmg);
       		QuickHeal_debug(string.format("装备治疗效果: %d", Bonus));
    		end
		BCStime=GetTime()
	end

    -- Spiritual Guidance - Increases spell damage and healing by up to 5% (per rank) of your total Spirit.
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,12);
    local _,Spirit,_,_ = UnitStat('player',5);
    local sgMod = Spirit * 5*talentRank/100;
    QuickHeal_debug(string.format("Spiritual Guidance Bonus: %f", sgMod));

    -- Calculate healing bonus
    local healMod15 = (1.5/3.5) * (sgMod + Bonus);
    local healMod20 = (2.0/3.5) * (sgMod + Bonus);
    local healMod25 = (2.5/3.5) * (sgMod + Bonus);
    local healMod30 = (3.0/3.5) * (sgMod + Bonus);
    QuickHeal_debug("Final Healing Bonus (1.5,2.0,2.5,3.0)", healMod15,healMod20,healMod25,healMod30);

    local InCombat = UnitAffectingCombat('player') or incombat;

    -- Spiritual Healing - Increases healing by 6% per rank on all spells
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,15);
    local shMod = 6*talentRank/100 + 1;
    QuickHeal_debug(string.format("Spiritual Healing modifier: %f", shMod));

    -- Improved Healing - Decreases mana usage by 5% per rank on LH,H and GH
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,11);
    local ihMod = 1 - 5*talentRank/100;
    QuickHeal_debug(string.format("Improved Healing modifier: %f", ihMod));

    local TargetIsHealthy = Health >= QuickHealVariables.RatioHealthyPriest;
    local ManaLeft = UnitMana('player');

    if TargetIsHealthy then
        QuickHeal_debug("Target is healthy",Health);
    end

    -- Detect proc of 'Hand of Edward the Odd' mace (next spell is instant cast)
    if QuickHeal_DetectBuff('player',"Spell_Holy_SearingLight") then
        QuickHeal_debug("BUFF: Hand of Edward the Odd (out of combat healing forced)");
        InCombat = false;
    end

    -- Detect Inner Focus or Spirit of Redemption (hack ManaLeft and healneed)
    if QuickHeal_DetectBuff('player',"Spell_Frost_WindWalkOn",1) or QuickHeal_DetectBuff('player',"Spell_Holy_GreaterHeal") then
        QuickHeal_debug("Inner Focus or Spirit of Redemption active");
        ManaLeft = UnitManaMax('player'); -- Infinite mana
        healneed = 10^6; -- Deliberate overheal (mana is free)
    end

    -- Get total healing modifier (factor) caused by healing target debuffs
    --local HDB = QuickHeal_GetHealModifier(Target);
    --QuickHeal_debug("Target debuff healing modifier",HDB);
    healneed = healneed/hdb;

    -- if forceMaxRank, feed it an obnoxiously large heal requirement
    --if forceMaxRank then
    --    print('lollololool');
    --    healneed = 10000;
    --end

    -- Get a list of ranks available for all spells
    local SpellIDsLH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_LESSER_HEAL);
    local SpellIDsH  = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_HEAL);
    local SpellIDsGH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_GREATER_HEAL);
    local SpellIDsFH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_FLASH_HEAL);
    local SpellIDsR = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_RENEW);

    local maxRankLH = table.getn(SpellIDsLH);
    local maxRankH  = table.getn(SpellIDsH);
    local maxRankGH = table.getn(SpellIDsGH);
    local maxRankFH = table.getn(SpellIDsFH);
    local maxRankR = table.getn(SpellIDsR);

    QuickHeal_debug(string.format("Found LH up to rank %d, H up top rank %d, GH up to rank %d, FH up to rank %d, and R up to max rank %d", maxRankLH, maxRankH, maxRankGH, maxRankFH, maxRankR));

    --Get max HealRanks that are allowed to be used
    local downRankFH = QuickHealVariables.DownrankValueFH  -- rank for 1.5 sec heals
    local downRankNH = QuickHealVariables.DownrankValueNH -- rank for < 1.5 sec heals

    -- Compensation for health lost during combat
    local k=1.0;
    local K=1.0;
    if InCombat then
        k=0.9;
        K=0.8;
    end

    SpellID = SpellIDsR[1]; HealSize = (45+healMod15)*shMod; -- Default to Renew(Rank 1)
    if healneed > (100+healMod15)*k*shMod and ManaLeft >= 155 and maxRankR >=2  and SpellIDsR[2]  then SpellID = SpellIDsR[2];  HealSize = (100+healMod15)*shMod end
    if healneed > (175+healMod15)*k*shMod and ManaLeft >= 185 and maxRankR >=3  and SpellIDsR[3]  then SpellID = SpellIDsR[3];  HealSize = (175+healMod15)*shMod end
    if healneed > (245+healMod15)*k*shMod and ManaLeft >= 215 and maxRankR >=4  and SpellIDsR[4]  then SpellID = SpellIDsR[4];  HealSize = (245+healMod15)*shMod end
    if healneed > (270+healMod15)*k*shMod and ManaLeft >= 265 and maxRankR >=5  and SpellIDsR[5]  then SpellID = SpellIDsR[5];  HealSize = (270+healMod15)*shMod end
    if healneed > (340+healMod15)*k*shMod and ManaLeft >= 315 and maxRankR >=6  and SpellIDsR[6]  then SpellID = SpellIDsR[6];  HealSize = (340+healMod15)*shMod end
    if healneed > (435+healMod15)*k*shMod and ManaLeft >= 380 and maxRankR >=7  and SpellIDsR[7]  then SpellID = SpellIDsR[7];  HealSize = (435+healMod15)*shMod end
    if healneed > (555+healMod15)*k*shMod and ManaLeft >= 455 and maxRankR >=8  and SpellIDsR[8]  then SpellID = SpellIDsR[8];  HealSize = (555+healMod15)*shMod end
    if healneed > (690+healMod15)*k*shMod and ManaLeft >= 545 and maxRankR >=9  and SpellIDsR[9]  then SpellID = SpellIDsR[9];  HealSize = (690+healMod15)*shMod end
    if healneed > (825+healMod15)*k*shMod and ManaLeft >= 655 and maxRankR >=10 and SpellIDsR[10] then SpellID = SpellIDsR[10]; HealSize = (825+healMod15)*shMod end
    QuickHeal_debug(string.format("选用技能ID：%d，技能治疗量：%d",SpellID,HealSize*HDB))
    return SpellID,HealSize*hdb;
end

function QuickHealSpellID(healneed)
    local SpellID = nil;
    local HealSize = 0;

    -- +Healing-PenaltyFactor = (1-((20-LevelLearnt)*0.0375)) for all spells learnt before level 20
    local PF1 = 0.2875;
    local PF4 = 0.4;
    local PF10 = 0.625;
    local PF18 = 0.925;

    -- Determine health and healneed of target
    --local healneed;
    --local Health;

    --if QuickHeal_UnitHasHealthInfo(Target) then
    --    -- Full info available
    --    healneed = UnitHealthMax(Target) - UnitHealth(Target) - HealComm:getHeal(UnitName(Target)); -- Implementatin for HealComm
    --
    --    Health = UnitHealth(Target) / UnitHealthMax(Target);
    --else
    --    -- Estimate target health
    --    healneed = QuickHeal_EstimateUnitHealNeed(Target,true); -- needs HealComm implementation maybe
    --
    --    Health = UnitHealth(Target)/100;
    --end

--by Crazydru 增加BCS进行统计，kook区更新的BCS统计更准确

	BCStime=BCStime or GetTime()-20
	if GetTime()-BCStime>10 then
   	 -- if BonusScanner is running, get +Healing bonus
    		if (BonusScanner) then
        		Bonus = tonumber(BonusScanner:GetBonus("HEAL"));
        		QuickHeal_debug(string.format("Equipment Healing Bonus: %d", Bonus));
    		end
		if BCS then
			local power,_,_,dmg = BCS:GetSpellPower()
			local heal = BCS:GetHealingPower()
			if dmg == nil then
				power,_,_,dmg = BCS:GetLiveSpellPower()
				heal = BCS:GetLiveHealingPower()
			end				
            heal = heal or 0
            power = power or 0
            dmg = dmg or 0
        	Bonus = tonumber(heal)+tonumber(power)-tonumber(dmg);
       		QuickHeal_debug(string.format("装备治疗效果: %d", Bonus));
    		end
		BCStime=GetTime()
	end

    -- Spiritual Guidance - Increases spell damage and healing by up to 5% (per rank) of your total Spirit.
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,12);
    local _,Spirit,_,_ = UnitStat('player',5);
    local sgMod = Spirit * 5*talentRank/100;
    QuickHeal_debug(string.format("Spiritual Guidance Bonus: %f", sgMod));

    -- Calculate healing bonus
    local healMod15 = (1.5/3.5) * (sgMod + Bonus);
    local healMod20 = (2.0/3.5) * (sgMod + Bonus);
    local healMod25 = (2.5/3.5) * (sgMod + Bonus);
    local healMod30 = (3.0/3.5) * (sgMod + Bonus);
    QuickHeal_debug("Final Healing Bonus (1.5,2.0,2.5,3.0)", healMod15,healMod20,healMod25,healMod30);

    local InCombat = UnitAffectingCombat('player');

    -- Spiritual Healing - Increases healing by 6% per rank on all spells
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,15);
    local shMod = 6*talentRank/100 + 1;
    QuickHeal_debug(string.format("Spiritual Healing modifier: %f", shMod));

    -- Improved Healing - Decreases mana usage by 5% per rank on LH,H and GH
    local _,_,_,_,talentRank,_ = GetTalentInfo(2,11);
    local ihMod = 1 - 5*talentRank/100;
    QuickHeal_debug(string.format("Improved Healing modifier: %f", ihMod));

    local ManaLeft = UnitMana('player');

    --if TargetIsHealthy then
    --    QuickHeal_debug("Target is healthy",Health);
    --end

    -- Detect proc of 'Hand of Edward the Odd' mace (next spell is instant cast)
    if QuickHeal_DetectBuff('player',"Spell_Holy_SearingLight") then
        --QuickHeal_debug("BUFF: Hand of Edward the Odd (out of combat healing forced)");
        InCombat = false;
    end

    -- Detect Inner Focus or Spirit of Redemption (hack ManaLeft and healneed)
    if QuickHeal_DetectBuff('player',"Spell_Frost_WindWalkOn",1) or QuickHeal_DetectBuff('player',"Spell_Holy_GreaterHeal") then
        --QuickHeal_debug("Inner Focus or Spirit of Redemption active");
        ManaLeft = UnitManaMax('player'); -- Infinite mana
        healneed = 10^6; -- Deliberate overheal (mana is free)
    end

    -- Get a list of ranks available for all spells
    local SpellIDsLH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_LESSER_HEAL);
    local SpellIDsH  = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_HEAL);
    local SpellIDsGH = QuickHeal_GetSpellIDs(QUICKHEAL_SPELL_GREATER_HEAL);

    local maxRankLH = table.getn(SpellIDsLH);
    local maxRankH  = table.getn(SpellIDsH);
    local maxRankGH = table.getn(SpellIDsGH);

    --QuickHeal_debug(string.format("Found LH up to rank %d, H up top rank %d, GH up to rank %d, FH up to rank %d, and R up to max rank %d", maxRankLH, maxRankH, maxRankGH, maxRankFH, maxRankR));

    -- Compensation for health lost during combat
    local k=1.0;
    local K=1.0;
    if InCombat then
        k=0.9;
        K=0.8;
    end

    -- if healType = channel
    --jgpprint(healType)

    SpellID = SpellIDsLH[1]; HealSize = (53+healMod15*PF1)*shMod; -- Default to LH
    if healneed > (  84+healMod20*PF4 )*k*shMod and ManaLeft >=  45*ihMod and maxRankLH >=2 and SpellIDsLH[2] then SpellID = SpellIDsLH[2]; HealSize = (  84+healMod20*PF4 )*shMod end
    if healneed > ( 154+healMod25*PF10)*K*shMod and ManaLeft >=  75*ihMod and maxRankLH >=3 and SpellIDsLH[3] then SpellID = SpellIDsLH[3]; HealSize = ( 154+healMod25*PF10)*shMod end
    if healneed > ( 330+healMod30*PF18)*K*shMod and ManaLeft >= 155*ihMod and maxRankH  >=1 and SpellIDsH[1]  then SpellID = SpellIDsH[1] ; HealSize = ( 330+healMod30*PF18)*shMod end
    if healneed > ( 476+healMod30     )*K*shMod and ManaLeft >= 205*ihMod and maxRankH  >=2 and SpellIDsH[2]  then SpellID = SpellIDsH[2] ; HealSize = ( 476+healMod30     )*shMod end
    if healneed > ( 624+healMod30     )*K*shMod and ManaLeft >= 255*ihMod and maxRankH  >=3 and SpellIDsH[3]  then SpellID = SpellIDsH[3] ; HealSize = ( 624+healMod30     )*shMod end
    if healneed > ( 667+healMod30     )*K*shMod and ManaLeft >= 305*ihMod and maxRankH  >=4 and SpellIDsH[4]  then SpellID = SpellIDsH[4] ; HealSize = ( 667+healMod30     )*shMod end
    if healneed > ( 838+healMod30     )*K*shMod and ManaLeft >= 370*ihMod and maxRankGH >=1 and SpellIDsGH[1] then SpellID = SpellIDsGH[1]; HealSize = ( 838+healMod30     )*shMod end
    if healneed > (1066+healMod30     )*K*shMod and ManaLeft >= 455*ihMod and maxRankGH >=2 and SpellIDsGH[2] then SpellID = SpellIDsGH[2]; HealSize = (1066+healMod30     )*shMod end
    if healneed > (1328+healMod30     )*K*shMod and ManaLeft >= 545*ihMod and maxRankGH >=3 and SpellIDsGH[3] then SpellID = SpellIDsGH[3]; HealSize = (1328+healMod30     )*shMod end
    if healneed > (1632+healMod30     )*K*shMod and ManaLeft >= 655*ihMod and maxRankGH >=4 and SpellIDsGH[4] then SpellID = SpellIDsGH[4]; HealSize = (1632+healMod30     )*shMod end
    if healneed > (1768+healMod30     )*K*shMod and ManaLeft >= 710*ihMod and maxRankGH >=5 and SpellIDsGH[5] then SpellID = SpellIDsGH[5]; HealSize = (1768+healMod30     )*shMod end

    --return SpellID;

    -- Get spell info
    local SpellName, SpellRank = GetSpellName(SpellID, BOOKTYPE_SPELL);
    if SpellRank == "" then
        SpellRank = nil
    end
    local SpellNameAndRank = SpellName .. (SpellRank and " (" .. SpellRank .. ")" or "");

    QuickHeal_debug("  Casting: " .. SpellNameAndRank .. " on " .. " NOPE " .. ", ID: " .. SpellID);

    local s = "Rank13";

    --msg = string.gsub(msg, "^%s*(.-)%s*$", "%1")

    local out = string.gsub(SpellRank,"%a+", "");
    --QuickHeal_debug("  out: " .. out);
    QuickHeal_debug(string.format(SpellName..out))
    return SpellName, out;
end



function QuickHeal_Command_Priest(msg)

    --if PlayerClass == "priest" then
    --  writeLine("PRIEST", 0, 1, 0);
    --end

    local _, _, arg1, arg2, arg3 = string.find(msg, "%s?(%w+)%s?(%w+)%s?(%w+)")

    -- match 3 arguments
    if arg1 ~= nil and arg2 ~= nil and arg3 ~= nil then
        if arg1 == "player" or arg1 == "target" or arg1 == "targettarget" or arg1 == "party" or arg1 == "subgroup" or arg1 == "mt" or arg1 == "nonmt" then
            if arg2 == "heal" and arg3 == "max" then
                --writeLine(QuickHealData.name .. " qh " .. arg1 .. " HEAL(maxHPS)", 0, 1, 0);
                QuickHeal(arg1, nil, nil, true);
                return;
            end
            if arg2 == "hot" and arg3 == "fh" then
                --writeLine(QuickHealData.name .. " qh " .. arg1 .. " HOT(max rank & no hp check)", 0, 1, 0);
                QuickHOT(arg1, nil, nil, true, true);
                return;
            end
            if arg2 == "hot" and arg3 == "max" then
                --writeLine(QuickHealData.name .. " qh " .. arg1 .. " HOT(max rank)", 0, 1, 0);
                QuickHOT(arg1, nil, nil, true, false);
                return;
            end
        end
    end

    -- match 2 arguments
    local _, _, arg4, arg5= string.find(msg, "%s?(%w+)%s?(%w+)")

    if arg4 ~= nil and arg5 ~= nil then
        if arg4 == "debug" then
            if arg5 == "on" then
                QHV.DebugMode = true;
                --writeLine(QuickHealData.name .. " debug mode enabled", 0, 0, 1);
                return;
            elseif arg5 == "off" then
                QHV.DebugMode = false;
                --writeLine(QuickHealData.name .. " debug mode disabled", 0, 0, 1);
                return;
            end
        end
        if arg4 == "heal" and arg5 == "max" then
            --writeLine(QuickHealData.name .. " HEAL (max)", 0, 1, 0);
            QuickHeal(nil, nil, nil, true);
            return;
        end
        if arg4 == "hot" and arg5 == "max" then
            --writeLine(QuickHealData.name .. " HOT (max)", 0, 1, 0);
            QuickHOT(nil, nil, nil, true, false);
            return;
        end
        if arg4 == "hot" and arg5 == "fh" then
            --writeLine(QuickHealData.name .. " FH (max rank & no hp check)", 0, 1, 0);
            QuickHOT(nil, nil, nil, true, true);
            return;
        end
        if arg4 == "player" or arg4 == "target" or arg4 == "targettarget" or arg4 == "party" or arg4 == "subgroup" or arg4 == "mt" or arg4 == "nonmt" then
            if arg5 == "hot" then
                --writeLine(QuickHealData.name .. " qh " .. arg1 .. " HOT", 0, 1, 0);
                QuickHOT(arg1, nil, nil, false, false);
                return;
            end
            if arg5 == "heal" then
                --writeLine(QuickHealData.name .. " qh " .. arg1 .. " HEAL", 0, 1, 0);
                QuickHeal(arg1, nil, nil, false);
                return;
            end
        end
    end

    -- match 1 argument
    local cmd = string.lower(msg)

    if cmd == "cfg" then
        QuickHeal_ToggleConfigurationPanel();
        return;
    end

    if cmd == "toggle" then
        QuickHeal_Toggle_Healthy_Threshold();
        return;
    end

    if cmd == "downrank" or cmd == "dr" then
        ToggleDownrankWindow()
        return;
    end

    if cmd == "tanklist" or cmd == "tl" then
        QH_ShowHideMTListUI();
        return;
    end

    if cmd == "reset" then
        QuickHeal_SetDefaultParameters();
        writeLine(QuickHealData.name .. " reset to default configuration", 0, 0, 1);
        QuickHeal_ToggleConfigurationPanel();
        QuickHeal_ToggleConfigurationPanel();
        return;
    end

    if cmd == "heal" then
        --writeLine(QuickHealData.name .. " HEAL", 0, 1, 0);
        QuickHeal();
        return;
    end

    if cmd == "hot" then
        --writeLine(QuickHealData.name .. " HOT", 0, 1, 0);
        QuickHOT();
        return;
    end

    if cmd == "" then
        --writeLine(QuickHealData.name .. " qh", 0, 1, 0);
        QuickHeal(nil);
        return;
    elseif cmd == "player" or cmd == "target" or cmd == "targettarget" or cmd == "party" or cmd == "subgroup" or cmd == "mt" or cmd == "nonmt" then
        --writeLine(QuickHealData.name .. " qh " .. cmd, 0, 1, 0);
        QuickHeal(cmd);
        return;
    end

    -- Print usage information if arguments do not match
    --writeLine(QuickHealData.name .. " Usage:");
    writeLine("== QUICKHEAL USAGE : PRIEST ==");
    writeLine("/qh cfg - 打开配置面板.");
    writeLine("/qh toggle - 在迅捷治疗和一般治疗中开关切换 (健康阈值 0% 或 100%).");
    writeLine("/qh downrank | dr - 打开 QuickHeal 的低级法术治疗等级滑块.");
    writeLine("/qh tanklist | tl - 显示坦克列表");
    writeLine("/qh [mask] [type] [mod] - 用最适合的治疗法术来治疗最需要的小队/团队队员.");
    writeLine(" [mask] 限制治疗目标:");
    writeLine("  [player] 只治疗玩家");
    writeLine("  [target] 只治疗目标");
    writeLine("  [targettarget] 只治疗目标的目标");
    writeLine("  [party] 只治疗队伍");
    writeLine("  [mt] 只治疗MT");
    writeLine("  [nonmt] 只治疗非MT");
    writeLine("  [subgroup] 只治疗配置面板中选定的小组");

    writeLine(" [type] 治疗技能类型（[heal] or [hot]）");
    writeLine("  [heal] 读条治疗");
    writeLine("  [hot]  持续治疗");
    writeLine(" [mod] (可选) [heal] or [hot]模式下额外选项:");
    writeLine("  [heal] 可增加选项:");
    writeLine("   [max] 用最高级读条治疗技能治疗不满血目标");
    writeLine("  [hot] 可增加选项:");
    writeLine("   [max] 用最高级HOT治疗不满血团员");
    writeLine("   [fh] 用最高级HOT治疗目标，无视是否掉血");

    writeLine("/qh reset - Reset configuration to default parameters for all classes.");
end
